package br.com.beautycenter.view;

import Funcionalidades.Bean;
import br.com.beautycenter.DAO.PersisteTipoDespesa;
import br.com.beautycenter.Funcionalidades.Swing;
import br.com.beautycenter.Funcionalidades.Validacoes;
import br.com.beautycenter.bean.TipoDespesa;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

public class CadastrarTipoDespesa extends javax.swing.JDialog {

    public CadastrarTipoDespesa(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        inicializarTela();
    }
    
    PersisteTipoDespesa persisteTipoDespesa = new PersisteTipoDespesa();
    
    TipoDespesa tipoDespesa = new TipoDespesa();
    TipoDespesa tempTipoDespesa = new TipoDespesa();
    
    static List<TipoDespesa> listaTipoDespesas = new ArrayList<TipoDespesa>();
    
    private void ativarBotoesIniciar() {
        listaTipoDespesas = persisteTipoDespesa.retornarTipoDespesa();
        if (listaTipoDespesas.isEmpty()) {
            btnNovo.setEnabled(false);
            txtTipoDespesa.requestFocus();
        } else {
            btnNovo.setEnabled(true);
            btnSalvar.setEnabled(false);
            btnCancelar.setEnabled(false);
            limparCampos();
            desativarCampos();
        }
    }
    
    public void ativarBotoesSalvarCancelar(){
        btnNovo.setEnabled(false);
        btnSalvar.setEnabled(true);
        btnCancelar.setEnabled(true);
    }
    
    public boolean validarTipoDespesa(){
        if(Validacoes.validarCampoTexto(txtTipoDespesa.getText().trim())){
            return true;
        }else{
            JOptionPane.showMessageDialog(null, "O campo Tipo Despesa dever ser preenchido.");
            txtTipoDespesa.requestFocus();
            return false;
        }
    }
    
    public boolean validarNomeTipoDespesa(){
        tipoDespesa = new TipoDespesa();
        tipoDespesa.setTipoDespesa(txtTipoDespesa.getText().trim().toString());
                
        if(!persisteTipoDespesa.retornarTipoDespesaPeloNome(tipoDespesa).isEmpty()){
            JOptionPane.showMessageDialog(null, "ESTE TIPO DESPESA JÁ ESTA CADASTRADO NO BANCO DE DADOS");
            txtTipoDespesa.requestFocus();
            limparCampos();
            return false;
        }else{
            return true;
        }
    }
    
    public void limparCampos() {
        Swing.zerarCampos(jPanelCampos.getComponents());
        txtTipoDespesa.setText(null);
    }
    
    public void ativarCampos(){
        Swing.ativarCampos(jPanelCampos.getComponents());
    }
    
    public void desativarCampos() {
        Swing.desativarCampos(jPanelCampos.getComponents());
    }
    
    public void inicializarTela(){
        ativarBotoesIniciar();
    }
    
    public void novo(){
        limparCampos();
        ativarCampos();
        ativarBotoesSalvarCancelar();
        txtTipoDespesa.requestFocus();
    }
    
    public void salvar(){
        if(validarTipoDespesa() && validarNomeTipoDespesa()){
            tempTipoDespesa = new TipoDespesa();
            tempTipoDespesa.setTipoDespesa(txtTipoDespesa.getText().trim());

            Bean.copiarTipoDespesa(tempTipoDespesa, tipoDespesa);
            
            persisteTipoDespesa.salvar(tipoDespesa);
            
            JOptionPane.showMessageDialog(null, "O TIPO DESPESA FOI SALVO COM SUCESSO");
            limparCampos();
           
            ativarBotoesIniciar();
        }
    }
    
    public void cancelar(){
        listaTipoDespesas = persisteTipoDespesa.retornarTipoDespesa();
        
        limparCampos();
        if(!listaTipoDespesas.isEmpty()){
            desativarCampos();
        }
        ativarBotoesIniciar();
    }
    
    public void sair(){
        dispose();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelPrincipal = new javax.swing.JPanel();
        jPanelBotoes = new javax.swing.JPanel();
        btnNovo = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();
        jPanelCampos = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtTipoDespesa = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ERP BEAUTY CENTER - Cadastro tipo de despesas");
        setPreferredSize(new java.awt.Dimension(435, 155));

        jPanelPrincipal.setBackground(new java.awt.Color(255, 255, 255));
        jPanelPrincipal.setPreferredSize(new java.awt.Dimension(800, 300));

        jPanelBotoes.setPreferredSize(new java.awt.Dimension(800, 45));

        btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Novo.png"))); // NOI18N
        btnNovo.setToolTipText("NOVO");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Salvar.png"))); // NOI18N
        btnSalvar.setToolTipText("SALVAR");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("LIMPAR");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Sair.png"))); // NOI18N
        btnSair.setToolTipText("SAIR");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelBotoesLayout = new javax.swing.GroupLayout(jPanelBotoes);
        jPanelBotoes.setLayout(jPanelBotoesLayout);
        jPanelBotoesLayout.setHorizontalGroup(
            jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBotoesLayout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addComponent(btnNovo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalvar)
                .addGap(5, 5, 5)
                .addComponent(btnCancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSair)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelBotoesLayout.setVerticalGroup(
            jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelBotoesLayout.createSequentialGroup()
                .addGap(0, 4, Short.MAX_VALUE)
                .addGroup(jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnSalvar)
                        .addComponent(btnCancelar)
                        .addComponent(btnSair, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(btnNovo)))
        );

        jPanelCampos.setBackground(new java.awt.Color(255, 255, 255));
        jPanelCampos.setPreferredSize(new java.awt.Dimension(800, 250));

        jLabel1.setText("TIPO DESPESA:");

        javax.swing.GroupLayout jPanelCamposLayout = new javax.swing.GroupLayout(jPanelCampos);
        jPanelCampos.setLayout(jPanelCamposLayout);
        jPanelCamposLayout.setHorizontalGroup(
            jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCamposLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtTipoDespesa, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(33, Short.MAX_VALUE))
        );
        jPanelCamposLayout.setVerticalGroup(
            jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCamposLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtTipoDespesa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanelPrincipalLayout = new javax.swing.GroupLayout(jPanelPrincipal);
        jPanelPrincipal.setLayout(jPanelPrincipalLayout);
        jPanelPrincipalLayout.setHorizontalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelBotoes, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
            .addComponent(jPanelCampos, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
        );
        jPanelPrincipalLayout.setVerticalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addComponent(jPanelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelCampos, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 435, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        novo();
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        salvar();
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        cancelar();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        sair();
    }//GEN-LAST:event_btnSairActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanelBotoes;
    private javax.swing.JPanel jPanelCampos;
    private javax.swing.JPanel jPanelPrincipal;
    private javax.swing.JTextField txtTipoDespesa;
    // End of variables declaration//GEN-END:variables
}
